package cn.luyou.admin.controller;

import cn.luyou.admin.common.Result;
import cn.luyou.admin.dto.GenerateWrongQuestionsDTO;
import cn.luyou.admin.entity.TrainingPaper;
import cn.luyou.admin.entity.Question;
import cn.luyou.admin.entity.TrainingPaperQuestion;
import cn.luyou.admin.service.TrainingPaperService;
import cn.luyou.admin.utils.BaseContext;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/training-papers")
public class TrainingPaperController {

    @Autowired
    private TrainingPaperService trainingPaperService;

    @GetMapping
    public Result<IPage<TrainingPaper>> getTrainingPapers(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        return Result.success(trainingPaperService.getTrainingPapers(pageNum, pageSize, BaseContext.getCurrentId()));
    }

    @PostMapping("/wrong-questions")
    public Result<TrainingPaper> generateFromWrongQuestions(
            @RequestBody GenerateWrongQuestionsDTO generateDTO,
            HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        return Result.success(trainingPaperService.generateFromWrongQuestions(BaseContext.getCurrentId(), generateDTO.getQuestionCount(), generateDTO.getPaperName()));
    }

    @PostMapping("/random")
    public Result<TrainingPaper> generateRandomPaper(
            @RequestParam String subject,
            @RequestParam Integer questionCount,
            HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        return Result.success(trainingPaperService.generateRandomPaper(BaseContext.getCurrentId(), subject, questionCount));
    }

    @GetMapping("/{id}/questions")
    public Result<List<Question>> getTrainingPaperQuestions(@PathVariable Long id) {
        return Result.success(trainingPaperService.getTrainingPaperQuestions(id));
    }

    @PostMapping("/{id}/submit")
    public Result<Void> submitTrainingPaper(
            @PathVariable Long id,
            @RequestBody Map<Long, String> answers) {
        trainingPaperService.submitTrainingPaper(id, answers);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<TrainingPaper> getTrainingPaperDetail(@PathVariable Long id) {
        return Result.success(trainingPaperService.getTrainingPaperDetail(id));
    }
    @GetMapping("/{id}/answers")
    public Result<Map<Long, String>> getSavedAnswers(@PathVariable Long id) {
        Long userId = BaseContext.getCurrentId();
        return Result.success(trainingPaperService.getSavedAnswers(id, userId));
    }
    @GetMapping("/{id}/results")
    public Result<List<TrainingPaperQuestion>> getResults(@PathVariable Long id) {
        return Result.success(trainingPaperService.getPaperResults(id));
    }
} 